package source;

/*
This file is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License version 2, 1991 as published by
the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
details.

A copy of the GNU General Public License can be found at:
http://www.gnu.org/licenses/gpl.html
*/
import java.io.*;
/**
* <p>Handles all bans</p>
* <p>Created by Diablo1123</p>
*/
public class BanHandler {
	/**
	* <p>Max number of players/IPs to be banned</p
	*/
	public static int maxBanned = 500;
	/**
	* <p>Holds the name of banned players and IP of banned IPs</p>
	*/
	public static String Banned[] = new String[maxBanned];
	/**
	* <p>The number of people banned.</p>
	*/
	public static int BannedCount = 0;
	/**
	* <p>Loads the banned players</p>
	*/
	static {
		for(int i = 0; i < maxBanned; i++) {
			Banned[i] = null;
		}
		loadBanned("config\\banned.cfg");
	}
	/**
	* <p>Checks against all loaded instances to see if the IP or the name is banned</p>
	*
	* @param banned String
	*/
	public static boolean CheckBanned(String banned){
		try{
		for (int i = 0; i < maxBanned; i++)
			if (Banned[i].equalsIgnoreCase(banned))
				return true;
		}catch (Exception e){
			//If there is no bans
		}
		return false;
	}
	/**
	* <p>Loads all banned players from a file</p>
	*
	* @param FileName String
	*/
	public static boolean loadBanned(String FileName) {
		String line = "";
		String token = "";
		String token2 = "";
		String token2_2 = "";
		String[] token3 = new String[10];
		boolean EndOfFile = false;
		int ReadMode = 0;
		BufferedReader characterfile = null;
		try {
			characterfile = new BufferedReader(new FileReader("./"+FileName));
		} catch(FileNotFoundException fileex) {
			misc.println(FileName+": file not found.");
			return false;
		}
		try {
			line = characterfile.readLine();
		} catch(IOException ioexception) {
			misc.println(FileName+": error loading file.");
			return false;
		}
		while(EndOfFile == false && line != null) {
			line = line.trim();
			Banned[BannedCount] = line;
			BannedCount++;
			try {
				line = characterfile.readLine();
			} catch(IOException ioexception1) { EndOfFile = true; }
		}
		try { characterfile.close(); } catch(IOException ioexception) { }
		return false;
	}

}
